<template>
	<view class="page">
		<view class="page-bg"></view>
		<uni-nav-bar ref="navBar" statusBar title="火动商户" :border="false" backgroundColor="transparent"
		@clickLeft="goBack"	>
			<template v-slot:left>
				<image class="size-32" src="@/static/svg/icon-back.svg" />
			</template>
		</uni-nav-bar>
		<view class="content flex flex-col">
			<view class="invite-inner flex item-center">
				<image :src="merInfo.logo" mode="" class="invite-logo"></image>
				<view class="invite-name">
					{{merInfo.businessShortName}} 
				</view>
			</view>
			<image src="@/applyPages/static/invite-first.png" mode="widthFix" class="invite-first"></image>
			<image src="@/applyPages/static/invite-second.png" mode="widthFix" class="invite-second"></image>
			<image src="@/applyPages/static/invite-third.png" mode="widthFix" class="invite-third"></image>
			<image src="@/applyPages/static/invite-img.png" mode="widthFix" class="invite-img"></image>
			<view class="setBtn" @click="linkMerchantApp">成为火动商户</view>
		</view>
	</view>
</template>

<script setup>
	import {ref} from 'vue';
	import {onLoad} from '@dcloudio/uni-app'
	import {
		getByInviteCode,
		preApplyMerchant,
		getEnterInfo
	} from '@/api/businessEnterApply.js'
	import {
		useUserStore
	} from '@/stores/modules/user.js'
	import { parseScene } from '@/utils/index.js'
	import {
		authCodeApi,
		getWxMobileApi
	} from '@/api/auth.js'
	const userStore = useUserStore()
	const code = ref(null)
	
	const merInfo = ref({})
	const oldMerInfo = ref({})
	const token = uni.getStorageSync('token')
	const wxPhone = uni.getStorageSync('wxPhone')
	const stoOpenId = uni.getStorageSync('openId')
	const preInfo = ref({})
	const getInfo =  async () => {
		let result = await getByInviteCode({settleCode: code.value})
		if(result){
			merInfo.value = result
		}
	}
	const getMerInfo = async (stateResult) => {
		let result = await getEnterInfo({
			openid: uni.getStorageSync('openId')
		})
		if(result){
			oldMerInfo.value = result
			userStore.setMerchantInfo(result)
		}
	}
	const linkMerchantApp = () => {
		if(preInfo.value.state == 0 ||preInfo.value.state == 4){
			if(code.value != oldMerInfo.value.settleCode && preInfo.value.state == 4){
				// 如果不是同个邀请码进入的入驻页面，则需要重新进入新的入驻页面
				if (code.value && wxPhone) {
					let params = code.value ? `?code=${code.value}` : ``
					uni.redirectTo({
						url: `/applyPages/bussinessApply/bussinessAgreement${params}`
					})
				}else{
					let params = code.value ? `?code=${code.value}&re=busi` : ``
					uni.redirectTo({
						url: `/pages/loginCode/login${params}`
					})
				}
				return
			}
			if (token) {
				uni.reLaunch({
					url: '/homePages/pages/home/home'
				})
				// getMerInfo(preInfo.value)
			}else {
				if (code.value && wxPhone) {
					let params = code.value ? `?code=${code.value}` : ``
					uni.redirectTo({
						url: `/applyPages/bussinessApply/bussinessAgreement${params}`
					})
				}else{
					let params = code.value ? `?code=${code.value}&re=busi` : ``
					uni.redirectTo({
						url: `/pages/loginCode/login${params}`
					})
				}
			}
		}else if(preInfo.value.state == 3){
				// 已经入驻成功
				code.value = ''
				userStore.setInestCode(null)
				if (token) {
					uni.reLaunch({
						url: '/homePages/pages/home/home'
					})
				}else{
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}	
			// }else if(preInfo.value.state == 0){
			// 	let params = code.value ? `?code=${code.value}` : ``
			// 	uni.redirectTo({
			// 		url: `/applyPages/bussinessApply/bussinessAgreement${params}`
			// 	})
			// }
			// if(result.state == 0)
		}
	}
	
	const openId = ref('')
	const getOpenId = async () => {
		const res = await uni.login({
			provider: 'weixin'
		})
		if (res.code) {
			const result = await authCodeApi((res.code))
			if (result.openId) {
				// 已注册，直接跳转到首页
				// token.value = result.token
				// userStore.setToken(result.token)
				openId.value = result.openId
				uni.setStorageSync('openId', result.openId)
				getPreApply()
				getMerInfo()
			} 
		}	
	}
	// 有链接
	// 入驻状态：0-未申请，1-待审核，
	// 2-汇付审核失败(需要重新编辑提交上一个商户入驻记录)，
	// 3-审核成功(如果是审核成功，跳转到登录页面；如果是未申请，跳转到入驻页面；否则跳转到对应审核状态页面)，
	// 4-商家审核失败(如果不是同个邀请码进入的入驻页面，则需要重新进入新的入驻页面)
	const getPreApply = async () => {
		let result = await  preApplyMerchant({openid:openId.value,settleCode:code.value})
		// const token = uni.getStorageSync('token')
		// const wxPhone = uni.getStorageSync('wxPhone')
		if(result){
			preInfo.value = result
			// 该链接已经审核，看状态
			if(result.state ==1 || result.state ==2){
				uni.redirectTo({
					url: `/applyPages/bussinessApply/applyResult/applyResult?state=${result.state}`
				})
			}else if(result.state == 3){
				// 已经入驻成功
				code.value = ''
				userStore.setInestCode(null)
				if (token) {
					uni.reLaunch({
						url: '/homePages/pages/home/home'
					})
				}else{
					uni.reLaunch({
						url: '/pages/login/login'
					})
				}	
				// getMerInfo(preInfo.value)
			}else if(result.state == 0){
				if (token) {
					uni.reLaunch({
						url: '/homePages/pages/home/home'
					})
				}
			}
		}
	}
	const goBack = () => {
		if(getCurrentPages().length === 1) {
			if (token) {
				uni.reLaunch({
					url: '/homePages/pages/home/home'
				})
			}else{
				uni.reLaunch({
					url: '/pages/login/login'
				})
			}
			return
		}
		uni.navigateBack()
	}
	onLoad((query) => {
		if(query.code){
			code.value = query.code
		}
		// 扫码打开
		if (query.scene) {
			const scene = parseScene(query.scene)
			if (!query.code && scene.code) {
				code.value = scene.code
			}
		}
		userStore.setInestCode(code.value)
		getInfo()
		getOpenId()
		// getMerInfo()
	})
</script>

<style scoped lang="scss">
.page {
	position: relative;
	width: 100vw;
	height: 100vh;
}
.content{
	position: relative;
	z-index: 1;
}
.page-bg {
	z-index: 0;
	position: absolute;
	left: 0;
	right: 0;
	top: 0;
	bottom: 0;
	background-image: linear-gradient(180deg, #ECC68B 0%, #FD2F33 48.02%, #FA4543 100%);
	// background-image: radial-gradient(circle at left top, #D8230A26 50%, transparent 50%);
	background-size: 100% 100%;
	background-repeat: no-repeat;
	// filter: blur(50px);
}
.invite-inner{
	margin: 20px 30px;
	.invite-name{
		font-size: 16px;
		font-weight: 600;
		line-height: 22px;
		color: #ffffff;
		margin-left: 10px;
	}
}
.invite-logo{
	width: 46px;
	height: 46px;
	border: 1px solid #ffffff;
	border-radius: 50%;
}
.invite-first{
	width: 300rpx;
	margin: 0 30px;
	margin-bottom: 36rpx;
	
}
.invite-second{
	width: 240rpx;
	margin: 0 30px;
	margin-bottom: 36rpx;
}
.invite-third{
	width: 400rpx;
	margin: 0 30px;
	margin-bottom: 10px;
}
.invite-img{
	width: 650rpx;
	margin: 0rpx auto;
}
.setBtn{
	background: rgba(239, 185, 133, 1);
	height:46px;
	border-radius: 8px;
	font-family: PingFang SC;
	font-size: 16px;
	font-weight: 400;
	line-height: 46px;
	letter-spacing: 0px;
	text-align: center;
	color: #ffffff;
	margin: 0px 20px;
	position: fixed;
	bottom: 30px;
	width: calc(100% - 40px);
}
</style>
